package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionCommandItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.DisinfectionCommListItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
//소독명령서 쓰레드
public class DisinfectionCommandThread extends BasicThread {

	public interface DisinfectionCommandThreadCallback {
		public void OnDisinfectionCommandCompleted(boolean isNetworkConntected
				, DisinfectionCommandItem disfectionItem, String message);
		public void OnDisinfectionCommandError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionCommandThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String recNo;
	private String offCd;

	public DisinfectionCommandThread(Context context
			, DisinfectionCommandThreadCallback callback
			, String offCd
			, String recNo
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.recNo = recNo;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		recNo = recNo.substring(3);
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ024, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionCommandCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionCommandError(null);
				return;
			}

			try {		
				int result = json.getInt(Const.RESULT);
				
				if(result == 0){	// result 값이 0일 경우
					String data = json.getString(Const.DATA);
					
					ErrorItem errorItem = new ErrorItem();
					errorItem.setrDesc(data);

					callback.OnDisinfectionCommandError(errorItem);
				}else{	// result 값이 0이 아닐 경우
					JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
					JSONObject disinfection = arraydisinfection.getJSONObject(0);
					
					DisinfectionCommandItem disinfectionItem = new DisinfectionCommandItem();
					
					ArrayList<DisinfectionCommListItem> arrayEqu = new ArrayList<DisinfectionCommListItem>();
					JSONArray items	 	= json.getJSONArray(DisinfectionCommandItem.ITEMS);
					for(int i = 0; i < items.length(); i++) {
						JSONObject itemObject = items.getJSONObject(i);
						DisinfectionCommListItem item = new DisinfectionCommListItem();
						item.setFullRecNo(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.FULLRECNO)));
						item.setPrdlstCd(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.PRDLSTCD)));
						item.setPrdlstNm(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.PRDLSTNM)));
						item.setInsQty(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.INSQTY)));
						item.setFmtAntCmmdQty(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.FMTANTCMMDQTY)));
						item.setQunNm(BrUtilManager.isNullCheck(itemObject.getString(DisinfectionCommListItem.QUNNM)));
						arrayEqu.add(item);
					}
					disinfectionItem.setItems(arrayEqu);
					disinfectionItem.setItemsResult(BrUtilManager.isNullCheck(json.getString(DisinfectionCommandItem.ITEMSRESULT)));		
					disinfectionItem.setFullCmmdNo(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.FULLCMMDNO)));
					disinfectionItem.setFirstCmmdDe(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.FIRSTCMMDDE)));
					disinfectionItem.setUserNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.USERNM)));
					disinfectionItem.setStatus(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.STATUS)));
					disinfectionItem.setAntMetNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.ANTMETNM)));
					disinfectionItem.setActDefr(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.ACTDEFR)));
					disinfectionItem.setActDeto(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.ACTDETO)));
					disinfectionItem.setMedNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.MEDNM)));
					disinfectionItem.setMedEtc(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.MEDETC)));
					disinfectionItem.setAntRes(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.ANTRES)));
					disinfectionItem.setDbyhsScncenm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.DBYHSSCNCENM)));
					disinfectionItem.setGooArea(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.GOOAREA)));
					disinfectionItem.setWorArea(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionCommandItem.WORAREA)));
					
					callback.OnDisinfectionCommandCompleted(true, disinfectionItem, mResult);
				}
				
			} catch (JSONException e) {
				
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		
		
		post =  ParameterUtil.getDisinfectionPopupParams(offCd, recNo, 2);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
		
	}
}
